package com.archerdu.base.controller;

import java.io.IOException;
import java.io.Writer;
import java.util.Locale;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import com.archerdu.commons.constant.DataConstant;

/**
 * 资源国际化处理
 * 
 * @projectName springDemo
 * @description 资源国际化处理
 * @author archer du
 * @createDate 2013年9月27日 下午5:29:59
 * @updateUser dxf
 * @updateDate 2013年9月27日 下午5:29:59
 * @updateRemark 新建
 * @version 0.0.1
 */
@Controller
@RequestMapping("/common/i18n")
public class I18NController extends BaseController {

    private static final long serialVersionUID = -7380662267560246779L;

    /**
     * 设置本地化资源
     * 
     * @param session
     * @param localeString
     * @param writer
     * @param resp
     */
    @RequestMapping(value = "/local/{localeString}")
    public void setLocal(HttpSession session, @PathVariable String localeString, Writer writer, HttpServletResponse resp) {

        logger.debug("--- path variable local is " + localeString);
        resp.setContentType("text/plain");
        resp.setCharacterEncoding("utf-8");

        Locale locale = new Locale(localeString);
        session.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale);

        try {
            writer.write(DataConstant.COMMON_JSON_YES);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }
}
